#!/bin/bash
cur=$(cd "$(dirname "$0")"; pwd)
set +e

function packDir(){
    local src=$1
    dir=$(ls -l $src |awk '/^d/ {print $NF}')
    for i in $dir
    do
        echo "packing: $i"
        cd $src/$i
        #tar -zcf "$i.tgz" $i
        tar -cf "../$i.tgz" * #non-rootDir
    done 
}
packDir "$cur/resources/templates/dockerfiles"
packDir "$cur/resources/templates/layouts"

cd $cur
#gitAdd1
git add -A .
git commit -m "pack-commit"

#gitAdd2
commitID=$(git log --pretty=oneline |head -1 |awk '{print $1}')
commitID=${commitID:0:8}
commitMsg=$(git log --pretty=oneline |head -1 |awk '{print $2}')
if [ "pack-commit" == "$commitMsg" ]; then
    echo $commitID
    echo $commitID > $cur/pack-commitID.txt

    git add -A .
    git commit -m "commitID"
    git push
fi
# lastCommitID
# git log --pretty=oneline |head -2 |tail -1 |awk '{print $1}'